//
//  QQHelper.m
//  ShareTest
//
//  Created by Eric Wang on 15/1/31.
//  Copyright (c) 2015年 LinkMob. All rights reserved.
//

#import "QQHelper.h"
#import "TencentAppMacro.h"

@implementation QQHelper

+ (instancetype)shareInstance {
    
    static QQHelper *qqHelper = nil;
    @synchronized(self)
    {
        if (qqHelper == nil) {
            qqHelper = [[QQHelper alloc] init];
        }
    }
    
    return qqHelper;
}

- (instancetype)init {
    
    self = [super init];
    if (self) {
        _tencentOAuth = [[TencentOAuth alloc] initWithAppId:kQQAppID andDelegate:self];
    }
    
    return self;
}

- (void)shareTitle:(NSString *)title
              text:(NSString *)text
           imgData:(NSData *)imgData
            imgURL:(NSString *)imgURL
       description:(NSString *)desc
               url:(NSString *)url
          flashURL:(NSString *)flashURL
              type:(ShareType)type
             QZone:(BOOL)Qzone {
    
    switch (type) {
        case QQShareTypeText: {
            QQApiTextObject *txtObj = [QQApiTextObject objectWithText:text];
            SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:txtObj];
            // 分享到QQ
            QQApiSendResultCode sent = [QQApiInterface sendReq:req];
            
            [QQHelper shareResult:sent];
        }
            break;
        case QQShareTypeImage: {
            // 纯图片只能分享本地图片
            QQApiImageObject *imgObj = [QQApiImageObject objectWithData:imgData
                                                       previewImageData:imgData
                                                                  title:title
                                                            description:desc];
            SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:imgObj];
            
            QQApiSendResultCode sent = [QQApiInterface sendReq:req];
            
            [QQHelper shareResult:sent];
        }
            break;
        case QQShareTypeNews: {
            QQApiNewsObject *newsObj;
            if (imgData) {
                newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:url] title:title description:desc previewImageData:imgData];
            }
            else {
                newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:url] title:title description:desc previewImageURL:[NSURL URLWithString:imgURL]];
            }
           
            
            SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
            QQApiSendResultCode sent = Qzone ? [QQApiInterface SendReqToQZone:req] : [QQApiInterface sendReq:req];
            
            [QQHelper shareResult:sent];
            
        }
            break;
        case QQShareTypeAudio: {
            QQApiAudioObject *audioObj;
            if (imgData) {
                audioObj = [QQApiAudioObject objectWithURL:[NSURL URLWithString:url] title:title description:desc previewImageData:imgData];
            }
            else {
                audioObj = [QQApiAudioObject objectWithURL:[NSURL URLWithString:url] title:title description:desc previewImageURL:[NSURL URLWithString:imgURL]];
                
            }
            
            [audioObj setFlashURL:[NSURL URLWithString:flashURL]];
            SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:audioObj];
            QQApiSendResultCode sent = Qzone ? [QQApiInterface SendReqToQZone:req] : [QQApiInterface sendReq:req];
            
            [QQHelper shareResult:sent];
        }
            break;
        case QQShareTypeVideo: {
            QQApiVideoObject *videoObj;
            if (imgData) {
                videoObj = [QQApiVideoObject objectWithURL:[NSURL URLWithString:url] title:title description:desc previewImageData:imgData];
            }
            else {
                videoObj = [QQApiVideoObject objectWithURL:[NSURL URLWithString:url] title:title description:desc previewImageURL:[NSURL URLWithString:imgURL]];
            }
            
            [videoObj setFlashURL:[NSURL URLWithString:flashURL]];
            SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:videoObj];
            QQApiSendResultCode sent = Qzone ? [QQApiInterface SendReqToQZone:req] : [QQApiInterface sendReq:req];
            
            [QQHelper shareResult:sent];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - Utility
+ (NSData *)imageWithPath:(NSString *)imageFilePath {
    
    NSData *data = [NSData dataWithContentsOfFile:imageFilePath];
    return  data;
}

+ (NSURL *)imageWithURL:(NSString *)imageUrl {
    
    NSURL *imgURL = [NSURL URLWithString:imageUrl];
    
    return imgURL;
}

+ (void)shareResult:(QQApiSendResultCode)result {
    
    if (result == kOpenSDKErrorSuccess) {
        NSLog(@"分享成功");
    }
    else if (result == kOpenSDKErrorUnknown) {
        NSLog(@"未知错误");
    }
    else if (result == kOpenSDKErrorUserCancel) {
        NSLog(@"用户取消");
    }
    else if (result == kOpenSDKErrorReLogin) {
        NSLog(@"Token无效或者需要重新登录");
    }
    else if (result == kOpenSDKErrorOperationDeny) {
        NSLog(@"无操作权限");
    }
    else if (result == kOpenSDKErrorNetwork) {
        NSLog(@"网络错误，网络不通或连接不到服务器");
    }
    else if (result == kOpenSDKErrorURL) {
        NSLog(@"URL格式或协议错误");
    }
    else if (result == kOpenSDKErrorDataParse) {
        NSLog(@"数据解析错误，服务器返回的数据解析出错");
    }
    else if (result == kOpenSDKErrorParam) {
        NSLog(@"传入参数错误");
    }
    else if (result == kOpenSDKErrorConnTimeout) {
        NSLog(@"http连接超时");
    }
    else if (result == kOpenSDKErrorSecurity) {
        NSLog(@"安全问题");
    }
    else if (result == kOpenSDKErrorIO) {
        NSLog(@"下载和文件IO错误");
    }
    else if (result == kOpenSDKErrorServer) {
        NSLog(@"服务器端错误");
    }
    else if (result == kOpenSDKErrorWebPage) {
        NSLog(@"页面错误");
    }
    else {
        NSLog(@"图片过大 设置头像自定义错误码");
    }
}

#pragma mark - TencentSessionDelegate
//
// 登录成功
//
- (void)tencentDidLogin {
    
    if ([self.delegate respondsToSelector:@selector(qqLoginSuccessful:)]) {
        [self.delegate qqLoginSuccessful:_tencentOAuth];
    }
}

//
// 非网络问题导致登录失败
//
- (void)tencentDidNotLogin:(BOOL)cancelled {
    
    if ([self.delegate respondsToSelector:@selector(qqLoginUnsuccessful:)]) {
        [self.delegate qqLoginUnsuccessful:cancelled];
    }
}

//
// 网络错误导致登录失败
//
- (void)tencentDidNotNetWork {
    
    if ([self.delegate respondsToSelector:@selector(qqLoginNotNetwork)]) {
        [self.delegate qqLoginNotNetwork];
    }
}

//
// 获取用户信息
//
- (void)getUserInfoResponse:(APIResponse *)response {
    
    if ([self.delegate respondsToSelector:@selector(getUserInfo:)]) {
        [self.delegate getUserInfo:response];
    }
}



@end
